

import {
    GetGeneralDictList,
    GetCountryList,
} from "@/api/selectApis/selectApis";
import { dictEnum } from "./enums/dictEnum";
import { GetAirPortById } from "@/api/public";
import locale from "../i18n";
import hooks from "@/hooks";
const { t: t } = locale.global
interface ResultData {
    name: any
}
const res: any = ref([]);

/**
 * 获取国家
 */
export const getCountry = async () => {
    const { data } = await GetCountryList(dictEnum.country);
    res.value = data ?? []
}
/**
 * 字典从字符串转为数字
 * @param params 
 * @param dataName 
 * @returns 
 */
export const getDataStrToInt = async (params: any, dataName: any) => {
    const { isCn, isEn } = hooks.useLanguage()
    const result: ResultData = { name };
    if (!dataName) {
        return;
    }
    if (dataName === dictEnum.country) {
        const dd = res.value?.filter((r: any) => r.nameEn === params || r.nameZh === params || r.nameAr === params || r.id ===Number(params));
        if (dd && dd.length) {
            result.name = dd[0].id
            return result.name
        } else {
            return result.name;
        }
    } else {
        const res = await GetGeneralDictList({ dictKey: dataName });
        const dd = await res.data?.filter((r: any) => r.dictDesc === params || r.descZh === params || r.descAr === params || String(r.dictData) === String(params));
        if (dd && dd.length) {
            result.name = dd[0].dictData;
            return result.name
        } else {
            return result.name;
        }
    }
}



/**
 * 字典从数字转为字符串
 * @param params 
 * @param dataName 
 * @param proxy 
 * @returns 
 */
export const getDataIntToStr = async (params: any, dataName: any, array: any) => {
    // const { isEn, isCn } = new hooksIndex(proxy)
    const { isCn, isEn } = hooks.useLanguage()
    const result: ResultData = { name };
    if (!dataName) {
        return;
    }
    if (dataName === dictEnum.country) {
        const dd = res.value?.filter((r: any) => r.id === params);
        if (dd && dd.length) {
            result.name = isEn.value ? dd[0].nameEn : isCn.value ? dd[0].nameZh : dd[0].nameAr
            return result.name
        } else {
            return result.name;
        }
    } else {
        const nameArr = []
        const res = await GetGeneralDictList({ dictKey: dataName });
        const dd = await res.data?.filter((r: any) => parseInt(r.dictData) === params);
        if (dd && dd.length) {
            result.name = isEn.value ? dd[0].dictDesc : isCn.value ? dd[0].descZh : dd[0].descAr;
            return result.name
        } else {
            return result.name;
        }
    }

}

/**
 * 
 * @returns 获取国家公共方法
 */
export const getCountryList = async () => {
    return new Promise(async (resolve, reject) => {
        const { data, code, msg }: any = await GetCountryList(dictEnum.country);
        resolve(data);
        code !== 200 && reject(msg)
    })
}

/**
 * 处理机场数据
 * @param id 传入id参数
 */
export const dealWithAirportData = async (id: number) => {
    const { data }: any = await GetAirPortById(id).catch(error => {
        console.error(error);
    }).finally(() => { })
    return { data }
}